package com.njlime.cms.service.app;

import com.njlime.cms.entities.Community;
import com.njlime.cms.entities.CommunityComment;
import com.njlime.cms.entities.CommunityUp;
import com.njlime.cms.repository.app.AppCommunityDao;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.member.entiies.MemberComment;
import com.njlime.member.service.app.AppMemberCollectionService;
import com.njlime.member.service.app.AppMemberCommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Dean on 2017/10/10.
 */
@Service
public class AppCommunityService {
    @Resource
    AppCommunityDao appCommunityDao;
    @Resource
    AppCommunityUpService appCommunityUpService;
    @Resource
    AppCommunityCommentService appCommunityCommentService;
    @Resource
    AppCommunityCommentUpService appCommunityCommentUpService;
    @Resource
    AppMemberCollectionService appMemberCollectionService;
    @Resource
    AppMemberCommentService appMemberCommentService;

    /**
     * 查询
     * @param id
     * @return
     */
    public Community findOne(int id){
        return appCommunityDao.findOne(id);
    }
    /**
     * 详情
     * @param id
     * @param unit_id
     * @param uid
     * @return
     */
    public Map<String, Object> get(int id, Integer unit_id, Integer uid) {
        Map<String, Object> bean = appCommunityDao.get(id, unit_id);
        if(bean != null){
            PageBean pb = new PageBean();
            CommunityComment comment = new CommunityComment();
            comment.setCommunity_id(id);
            pb = commentList(pb, comment, uid);
            bean.put("comment", PageBean.transformListMap(pb));
            if (uid != null && uid > 0) {
                bean.put("collection", appMemberCollectionService.isCollected(uid,id,GlobalValue.BusinessType.COMMUNITY));
            }
            if (appCommunityUpService.isUped(id, uid)) {
                bean.put("is_up", "1");
            }
            PageBean rewardList = appCommunityCommentService.rewardList(new PageBean(6), id);
            bean.put("rewardList", PageBean.transformListMap(rewardList));
        }
        return bean;
    }

    /**
     * 贴吧列表
     * @param pageBean
     * @param info
     * @param uid
     * @return
     */
    public PageBean list(PageBean pageBean, Community info, Integer uid) {
        pageBean = appCommunityDao.list(pageBean, info);
        List<Map<String, Object>> communityList = new ArrayList<>();
        if(pageBean.getMapList() != null){
            for (Map<String, Object> commItem : pageBean.getMapList()) {
                //PageBean rewardList = appCommunityCommentService.rewardList(new PageBean(6), (Integer) commItem.get("id"));
                //commItem.put("rewardList", rewardList.getMapList());
                if(uid != null){
                    //当前用户是否点赞
                    Integer community_id = Integer.parseInt(commItem.get("id").toString());
                    // 判断是否点赞过这条
                    if (community_id != null && appCommunityUpService.isUped(community_id, uid)) {
                        commItem.put("is_up", 1);
                    } else {
                        commItem.put("is_up", 0);
                    }
                }
                communityList.add(commItem);
            }
        }
        pageBean.setMapList(communityList);
        return pageBean;
    }

    /**
     * 添加评论
     * @param communityComment
     */
    @Transactional
    public void addComment(CommunityComment communityComment,MemberComment memberComment){
        appCommunityCommentService.save(communityComment);
        if(memberComment != null) {
            appMemberCommentService.save(memberComment);
            //更新当前信息评论数
            updateCommentMum(communityComment.getCommunity_id(), 1);
        }
    }

    /**
     * 评论列表
     * @param pageBean
     * @param comment
     * @param uid
     * @return
     */
    public PageBean commentList(PageBean pageBean, CommunityComment comment, Integer uid){
        pageBean = appCommunityCommentService.list(pageBean, comment);
        if (pageBean.getMapList() != null && pageBean.getMapList().size() > 0) {
            List<Map<String, Object>> list = pageBean.getMapList();
            if(uid != null){
                for (Map<String, Object> map : list) {
                    Integer comment_id = (Integer) map.get("id");
                    // 判断是否点赞过这条
                    if (comment_id!= null && appCommunityCommentUpService.isUped(comment_id, uid)) {
                        map.put("is_up", 1);
                    } else {
                        map.put("is_up", 0);
                    }
                }
            }
        }
        return pageBean;
    }

    /**
     * 点赞
     * @param communityUp
     */
    @Transactional
    public void addUp(CommunityUp communityUp){
        appCommunityUpService.save(communityUp);
        fabulousNumUpdate(communityUp.getCommunity_id(), 1);
    }

    /**
     * 阅读数量 +1
     *
     * @param id 文章id
     * @return
     */
    public int updateReading(int id, Integer unit_id) {
        return appCommunityDao.updateReading(id, unit_id);
    }

    /**
     * 点赞数
     *
     * @param id
     */
    public void fabulousNumUpdate(Integer id, int num) {
        appCommunityDao.fabulousNumUpdate(id, num);
    }

    /**
     * 评论数
     * @param community_id
     * @param num
     */
    public void updateCommentMum(Integer community_id, int num) {
        appCommunityDao.updateCommentMum(community_id, num);
    }
}
